package LandR.personal.personalHelp.function_jsp;


import LandR.personal.personalHelp.Users.User;
import LandR.personal.personalHelp.function_mysql.ConnectionMethod;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 这个类包含了一些辅助JspPageUseMethod类的一些方法
 * 1）根据用户名确定用户是否是新用户 judgeUsernameOldOrNew
 * 2）根据手机号确定用户是否是新用户 judgePhoneNumberOldOrNew
 * 3）根据邮箱确定用户是否是新用户   judgeEmailOldOrNew
 * 4）根据id确定用户是否是新用户   judgeIdOldOrNew
 * 5）根据username返回用户的id   selectUserByUsername
 */

public class JspPageUseHelpMethod {
    /**
     * judgeUsernameOldOrNew()
     * 根据传入的用户对象的username判断在数据库中是否有相应的用户
     * 返回布尔类型
     *
     * @param user
     * @return boolean
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public boolean judgeUsernameOldOrNew(User user) throws SQLException, ClassNotFoundException {
        Connection conn = ConnectionMethod.getConnection();
        ResultSet rs = null;
        String sql = "select username from usersdata where username = ?";
        PreparedStatement ppst = ConnectionMethod.getPrepareStatement(sql);
        ppst.setString(1, user.getUsername());
        System.out.println("信息定位成功");
        rs = (ResultSet) ppst.executeQuery();
        System.out.println("查询信息上传成功");
        ConnectionMethod connMethod = new ConnectionMethod();
        if (rs.next()) {
            System.out.println("在该username下确有用户");
            ConnectionMethod.close(rs);
            ConnectionMethod.close(ppst);
            ConnectionMethod.close(conn);
            System.out.println("资源释放成功");
            return true;
        } else {
            System.out.println("在该username下没有用户");
            ConnectionMethod.close(rs);
            ConnectionMethod.close(ppst);
            ConnectionMethod.close(conn);
            System.out.println("资源释放成功");
            return false;
        }
    }

    /**
     * judgePhoneNumberOldOrNew(User user)
     * 根据传入参数的PhoneNumber来查询数据库中是否有该用户
     * 返回布尔类型
     *
     * @param user
     * @return boolean
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public boolean judgePhoneNumberOldOrNew(User user) throws SQLException, ClassNotFoundException {
        Connection conn = ConnectionMethod.getConnection();
        ResultSet rs = null;
        String sql = "select username from usersdata where phonenumber = ?";
        PreparedStatement ppst = ConnectionMethod.getPrepareStatement(sql);
        ppst.setString(1, user.getPhoneNumber());
        System.out.println("信息定位成功");
        rs = (ResultSet) ppst.executeQuery();
        System.out.println("查询信息上传成功");
        ConnectionMethod connMethod = new ConnectionMethod();
        if (rs.next()) {
            System.out.println("在该PhoneNumber下确有用户");
            ConnectionMethod.close(rs);
            ConnectionMethod.close(ppst);
            ConnectionMethod.close(conn);
            System.out.println("资源释放成功");
            return true;
        } else {
            System.out.println("在该PhoneNumber下没有用户");
            ConnectionMethod.close(rs);
            ConnectionMethod.close(ppst);
            ConnectionMethod.close(conn);
            System.out.println("资源释放成功");
            return false;
        }
    }

    /**
     * judgeEmailOldOrNew()
     * 根据传入的用户对象的username判断在数据库中是否有相应的用户
     * 返回布尔类型
     *
     * @param user
     * @return boolean
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public boolean judgeEmailOldOrNew(User user) throws SQLException, ClassNotFoundException {
        Connection conn = ConnectionMethod.getConnection();
        ResultSet rs = null;
        String sql = "select username from usersdata where email = ?";
        PreparedStatement ppst = ConnectionMethod.getPrepareStatement(sql);
        ppst.setString(1, user.getEmail());
        System.out.println("信息定位成功");
        rs = (ResultSet) ppst.executeQuery();
        System.out.println("查询信息上传成功");
        ConnectionMethod connMethod = new ConnectionMethod();
        if (rs.next()) {
            System.out.println("在该email下确有用户");
            ConnectionMethod.close(conn);
            ConnectionMethod.close(ppst);
            ConnectionMethod.close(rs);
            System.out.println("资源释放成功");
            return true;
        } else {
            System.out.println("在该email下没有用户");
            ConnectionMethod.close(rs);
            ConnectionMethod.close(ppst);
            ConnectionMethod.close(conn);


            System.out.println("资源释放成功");
            return false;
        }
    }

    /**
     * boolean judgeIdOldOrNew(User user)
     * 传入一个user对象作为参数
     * 通过user对象的id属性判断是否有这个用户
     *
     * @param user
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public boolean judgeIdOldOrNew(User user) throws SQLException, ClassNotFoundException {
        Connection conn = ConnectionMethod.getConnection();
        ResultSet rs = null;
        String sql = "select id from usersdata where id = ?";
        PreparedStatement ppst = ConnectionMethod.getPrepareStatement(sql);
        ppst.setString(1, user.getId().toString());
        System.out.println("信息定位成功");
        rs = (ResultSet) ppst.executeQuery();
        System.out.println("查询信息上传成功");
        ConnectionMethod connMethod = new ConnectionMethod();
        if (rs.next()) {
            System.out.println("在该id下确有用户");
            ConnectionMethod.close(rs);
            ConnectionMethod.close(ppst);
            ConnectionMethod.close(conn);
            System.out.println("资源释放成功");
            return true;
        } else {
            System.out.println("在该id下没有用户");
            ConnectionMethod.close(rs);
            ConnectionMethod.close(ppst);
            ConnectionMethod.close(conn);
            System.out.println("资源释放成功");
            return false;
        }
    }

    /**
     * int selectUserByUsername(String username)
     * 传入username作为参数
     * 返回这个username下的id
     *
     * @param username
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public int selectUserByUsername(String username) throws SQLException, ClassNotFoundException {
        Connection conn = ConnectionMethod.getConnection();
        ResultSet rs = null;
        String sql = "select * from usersdata where username = ?";
        PreparedStatement ppst = ConnectionMethod.getPrepareStatement(sql);
        ppst.setString(1, username);
        System.out.println("信息定位成功");
        rs = (ResultSet) ppst.executeQuery();
        System.out.println("查询信息上传成功");
        ConnectionMethod connMethod = new ConnectionMethod();
        int id = 0;
        while (rs.next()) {
            id = rs.getInt("id");
        }
        ConnectionMethod.close(rs);
        ConnectionMethod.close(ppst);
        ConnectionMethod.close(conn);
        System.out.println("资源释放成功");
        return id;
    }

}